!*************************************************************************************************************
! This subroutine generates the nearest grid point for the updated prior. The first argument in min_phi 
! is the grid pt number of the prior, the second argument is the level and the third argument is performance
!*************************************************************************************************************

SUBROUTINE sub_min_phi(min_phi)

USE mod_parameters
USE mod_types
USE mod_grid
USE mod_update

IMPLICIT NONE

INTEGER, DIMENSION (phi_grid_size,3,2):: min_phi

INTEGER:: temp, vi1,vi2

REAL(dp):: diff_1, diff_2


CALL grid_generation  


DO vi1 = 1, phi_grid_size 

!-------------------------------------------
!  Level=1, Rating=2
!-------------------------------------------
    
    IF (phi_update(phi_grid(vi1),1,2)<phi_grid(1)) THEN
       
                
        min_phi(vi1,1,2)=phi_grid(1)
        
           
    
    ELSE IF (phi_update(phi_grid(vi1),1,2)>phi_grid(phi_grid_size)) THEN
 
        min_phi(vi1,1,2)=phi_grid(phi_grid_size)
        
    ELSE 
        
        temp=0
        vi2=1
        
        DO WHILE (temp==0)
    
            vi2=vi2+1
            
            IF (phi_update(phi_grid(vi1),1,2)<=phi_grid(vi2)) THEN
                
                
                temp=1
                diff_1=ABS(phi_update(phi_grid(vi1),1,2)-phi_grid(vi2))
                diff_2=ABS(phi_update(phi_grid(vi1),1,2)-phi_grid(vi2-1))
                
                IF (diff_1>diff_2) THEN
                
                    min_phi(vi1,1,2)=vi2-1
                    
                END IF
                
                IF (diff_1<=diff_2) THEN
                
                    min_phi(vi1,1,2)=vi2
                    
                END IF
                
            END IF
            
        END DO
        
    END IF
    
!-------------------------------------------
!  Level=1, Rating=1
!-------------------------------------------

    
    IF (phi_update(phi_grid(vi1),1,1)<phi_grid(1)) THEN
       
                
        min_phi(vi1,1,1)=phi_grid(1)
        
           
    
    ELSE IF (phi_update(phi_grid(vi1),1,1)>phi_grid(phi_grid_size)) THEN
 
        min_phi(vi1,1,1)=phi_grid(phi_grid_size)
        
    ELSE 
        
        temp=0
        vi2=1
        
        DO WHILE (temp==0)
    
            vi2=vi2+1
            
            IF (phi_update(phi_grid(vi1),1,1)<=phi_grid(vi2)) THEN
                
                
                temp=1
                diff_1=ABS(phi_update(phi_grid(vi1),1,1)-phi_grid(vi2))
                diff_2=ABS(phi_update(phi_grid(vi1),1,1)-phi_grid(vi2-1))
                
                IF (diff_1>diff_2) THEN
                
                    min_phi(vi1,1,1)=vi2-1
                    
                END IF
                
                IF (diff_1<=diff_2) THEN
                
                    min_phi(vi1,1,1)=vi2
                    
                END IF
                
            END IF
            
        END DO
        
    END IF
    


!-------------------------------------------
!  Level=2, Rating=2
!-------------------------------------------
    
    IF (phi_update(phi_grid(vi1),2,2)<phi_grid(1)) THEN
       
                
        min_phi(vi1,2,2)=phi_grid(1)
        
           
    
    ELSE IF (phi_update(phi_grid(vi1),2,2)>phi_grid(phi_grid_size)) THEN
 
        min_phi(vi1,2,2)=phi_grid(phi_grid_size)
        
    ELSE 
        
        temp=0
        vi2=1
        
        DO WHILE (temp==0)
    
            vi2=vi2+1
            
            IF (phi_update(phi_grid(vi1),2,2)<=phi_grid(vi2)) THEN
                
                
                temp=1
                diff_1=ABS(phi_update(phi_grid(vi1),2,2)-phi_grid(vi2))
                diff_2=ABS(phi_update(phi_grid(vi1),2,2)-phi_grid(vi2-1))
                
                IF (diff_1>diff_2) THEN
                
                    min_phi(vi1,2,2)=vi2-1
                    
                END IF
                
                IF (diff_1<=diff_2) THEN
                
                    min_phi(vi1,2,2)=vi2
                    
                END IF
                
            END IF
            
        END DO
        
    END IF
    


!-------------------------------------------
!  Level=2, Rating=1
!-------------------------------------------
    
    IF (phi_update(phi_grid(vi1),2,1)<phi_grid(1)) THEN
       
                
        min_phi(vi1,2,1)=phi_grid(1)
        
           
    
    ELSE IF (phi_update(phi_grid(vi1),2,1)>phi_grid(phi_grid_size)) THEN
 
        min_phi(vi1,2,1)=phi_grid(phi_grid_size)
        
    ELSE 
        
        temp=0
        vi2=1
        
        DO WHILE (temp==0)
    
            vi2=vi2+1
            
            IF (phi_update(phi_grid(vi1),2,1)<=phi_grid(vi2)) THEN
                
                
                temp=1
                diff_1=ABS(phi_update(phi_grid(vi1),2,1)-phi_grid(vi2))
                diff_2=ABS(phi_update(phi_grid(vi1),2,1)-phi_grid(vi2-1))
                
                IF (diff_1>diff_2) THEN
                
                    min_phi(vi1,2,1)=vi2-1
                    
                END IF
                
                IF (diff_1<=diff_2) THEN
                
                    min_phi(vi1,2,1)=vi2
                    
                END IF
                
            END IF
            
        END DO
        
    END IF

!-------------------------------------------
!  Level=3, Rating=2
!-------------------------------------------    
    
    IF (phi_update(phi_grid(vi1),3,2)<phi_grid(1)) THEN
       
                
        min_phi(vi1,3,2)=phi_grid(1)
        
           
    
    ELSE IF (phi_update(phi_grid(vi1),3,2)>phi_grid(phi_grid_size)) THEN
 
        min_phi(vi1,3,2)=phi_grid(phi_grid_size)
        
    ELSE 
        
        temp=0
        vi2=1
        
        DO WHILE (temp==0)
    
            vi2=vi2+1
            
            IF (phi_update(phi_grid(vi1),3,2)<=phi_grid(vi2)) THEN
                
                
                temp=1
                diff_1=ABS(phi_update(phi_grid(vi1),3,2)-phi_grid(vi2))
                diff_2=ABS(phi_update(phi_grid(vi1),3,2)-phi_grid(vi2-1))
                
                IF (diff_1>diff_2) THEN
                
                    min_phi(vi1,3,2)=vi2-1
                    
                END IF
                
                IF (diff_1<=diff_2) THEN
                
                    min_phi(vi1,3,2)=vi2
                    
                END IF
                
            END IF
            
        END DO
        
    END IF

!-------------------------------------------
!  Level=3, Rating=1
!-------------------------------------------
    
    IF (phi_update(phi_grid(vi1),3,1)<phi_grid(1)) THEN
       
                
        min_phi(vi1,3,1)=phi_grid(1)
        
           
    
    ELSE IF (phi_update(phi_grid(vi1),3,1)>phi_grid(phi_grid_size)) THEN
 
        min_phi(vi1,3,1)=phi_grid(phi_grid_size)
        
    ELSE 
        
        temp=0
        vi2=1
        
        DO WHILE (temp==0)
    
            vi2=vi2+1
            
            IF (phi_update(phi_grid(vi1),3,1)<=phi_grid(vi2)) THEN
                
                
                temp=1
                diff_1=ABS(phi_update(phi_grid(vi1),3,1)-phi_grid(vi2))
                diff_2=ABS(phi_update(phi_grid(vi1),3,1)-phi_grid(vi2-1))
                
                IF (diff_1>diff_2) THEN
                
                    min_phi(vi1,3,1)=vi2-1
                    
                END IF
                
                IF (diff_1<=diff_2) THEN
                
                    min_phi(vi1,3,1)=vi2
                    
                END IF
                
            END IF
            
        END DO
        
    END IF
    
    
END DO


END SUBROUTINE